﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Drawing;
using HOOMT.DAO;

namespace HOOMT
{
    class OriginalLineDAO : BaseDAO
    {
        private MemoryDAO mDAO = new MemoryDAO();
        
        public bool AddLine(OriginalLine line, string parentFormId )
        {

            try
            {
                this.DeleteLine(line);
                SQLiteCommand cmd = this.GetCmd();
                StringBuilder sql=new StringBuilder("insert into OriginalLine(id,startPointX,startPointY,endPointX,endPointY,type,parentFormId,startDiagramId,endDiagramId,lineName");
                if (line.SubForm != null)
                {
                    sql.Append(",subFormId");
                }

                sql.Append(") values(@id,@startPointX,@startPointY,@endPointX,@endPointY,@type,@parentFormId,@startDiagramId,@endDiagramId,@lineName ");
                if (line.SubForm!=null)
                {
                    sql.Append(",@subFormId");
                }
                sql.Append(")");
                cmd.CommandText = sql.ToString();
                cmd.Parameters.Add(new SQLiteParameter("id",line.id));
                cmd.Parameters.Add(new SQLiteParameter("startPointX", line.StartPoint.X));
                cmd.Parameters.Add(new SQLiteParameter("startPointY", line.StartPoint.Y));
                cmd.Parameters.Add(new SQLiteParameter("endPointX", line.EndPoint.X));
                cmd.Parameters.Add(new SQLiteParameter("endPointY", line.EndPoint.Y));

                cmd.Parameters.Add(new SQLiteParameter("type", line.GetType().ToString()));

                cmd.Parameters.Add(new SQLiteParameter("parentFormId", parentFormId));

                cmd.Parameters.Add(new SQLiteParameter("startDiagramId", line.StartDiagram.Id));
                cmd.Parameters.Add(new SQLiteParameter("endDiagramId", line.EndDiagram.Id));

                cmd.Parameters.Add(new SQLiteParameter("lineName", line.LineName));
                if (line.SubForm != null)
                {
                    cmd.Parameters.Add(new SQLiteParameter("subFormId", line.SubForm.id));
                }
               Console.Write(cmd.CommandText);
                cmd.ExecuteNonQuery();

                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            finally
            {
                this.conn.Close();
            }
        }

         public bool DeleteLine(OriginalLine line )
        {
            try
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "delete  from OriginalLine where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("id", line.id));
                cmd.ExecuteNonQuery();
                 return true;
            }
            catch (System.Exception ex)
            {
                
                Console.WriteLine(ex.ToString());
                return false;
            }
            finally
            {
                this.conn.Close();
            }
        }

         public List<OriginalLine> GetLineListByParentForm(OriginalModelForm form)
         {
             List<OriginalLine> list = new List<OriginalLine>();
             try
             {
                 SQLiteCommand cmd = this.GetCmd();
                 cmd.CommandText = "select * from OriginalLine where parentFormId=@parentFormId";
                 cmd.Parameters.Add(new SQLiteParameter("parentFormId", form.id));
                 SQLiteDataReader reader=cmd.ExecuteReader();
                 while (reader.Read())
                 {
                     Point startPoint = new Point(int.Parse(reader["startPointX"].ToString()), 
                         int.Parse(reader["startPointY"].ToString()));
                     Point endPoint = new Point(int.Parse(reader["endPointX"].ToString()),
                         int.Parse(reader["endPointY"].ToString()));

                     string type = this.GetSimpleType(reader["type"].ToString());
                     string id=reader["id"].ToString();
                     string startDiagramId=reader["startDiagramId"].ToString();
                     string endDiagramId=reader["endDiagramId"].ToString();
                     OriginalDiagram startDiagram=null;
                     OriginalDiagram endDiagram=null;
                     foreach (OriginalDiagram diagram in form.diagramList)
                     {
                         if (startDiagramId.Equals(diagram.Id))
                         {
                             startDiagram=diagram;
                         } 
                         else if(endDiagramId.Equals(diagram.Id))
                         {
                             endDiagram=diagram;
                         }
                     }

                     string lineName = reader["lineName"].ToString();
                     string subFormId = reader["subFormId"].ToString();

                     OriginalLine line=null;
                     if ("InformationFlow".Equals(type))
                     {
                         line = new InformationFlow(startPoint, endPoint,id,startDiagram,endDiagram);

                     }
                     else if ("StateTransformFlow".Equals(type))
                     {
                         line = new StateTransformFlow(startPoint, endPoint, id,startDiagram,endDiagram);
                     }
                     else if ("StaticComplexRelationship".Equals(type))
                     {
                          line = new StaticComplexRelationship(startPoint, endPoint, id,startDiagram,endDiagram);
                     }
                     else if ("StaticSimpleRelationship".Equals(type))
                     {
                          line = new StaticSimpleRelationship(startPoint, endPoint, id,startDiagram,endDiagram);
                     }

                     this.AddLineRelations(line, lineName, subFormId);
                     list.Add(line);
                 }
                 return list;
             }
             catch (System.Exception ex)
             {

                 Console.WriteLine(ex.ToString());
                 return null;
             }
             finally
             {
                 this.conn.Close();
             }
         }


         private void AddLineRelations(OriginalLine line ,String lineName, String subFormId) {
             if (lineName!=null)
             {
                 line.LineName = lineName;
             }
             if (subFormId!=null)
             {
                 OriginalModelForm subForm = mDAO.findFormById(subFormId);
                 line.SubForm = subForm;
             }
         }

    }
}
